<?php
require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/dbConnection.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/occupation.php';

class OccupationDao {
	private $_db;

	public function __construct() {
		$this->_db = new DbConnection();
		$this->_db->connect();
	}
	
	/**
	 *
	 * Get all the occupations in the db
	 */
	public function getOccupationList() {
		$query = "SELECT * FROM occupations";
		
		$result = $this->_db->executeQuery($query);
		
		if (!$result) {
			echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return;
		}
		
		$occupationList = Array();
		
		while ($row = mysql_fetch_assoc($result)){			
			$occupation = new OccupationModel();
			
			$occupation->setOccupationID($row['OccupationId']);
			$occupation->setOccupationName($row['Occupation']);
			$occupationList[$row['OccupationId']] = $occupation;
		}
		
		mysql_free_result($result);
		
		return $occupationList;
	}
	
	public function getOccupation($occId) {
		$query = sprintf("SELECT * FROM occupations WHERE OccupationId = '%u'", $occId);
		
		$result = $this->_db->executeQuery($query);
		
		if (!$result) {
			echo 'Could not run query: ' . ' ' . $query . ' ' . mysql_error();
		    return;
		}
		
		if ($row = mysql_fetch_assoc($result)) {			
			$occupation = new OccupationModel();
			$occupation->setOccupationID($row['OccupationId']);
			$occupation->setOccupationName($row['Occupation']);		
		}
		
		mysql_free_result($result);
		
		return $occupation;
	}
}
?>